package cn.edu.lcvc.ebuy_2023rj2.web.action.backstage.admin;

import cn.edu.lcvc.ebuy_2023rj2.model.Admin;
import cn.edu.lcvc.ebuy_2023rj2.model.common.ResponseData;
import cn.edu.lcvc.ebuy_2023rj2.service.AdminService;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.server.HttpServerResponse;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Locale;

@RestController
@RequestMapping(value = "/api/backstage/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @PostMapping("/photo")
    public ResponseData uploadPhoto(MultipartFile file, HttpServletRequest request, HttpSession session){
        ResponseData responseData=null;
        if (file!=null){
            if (StringUtils.hasLength(file.getOriginalFilename())){
                //图片重命名
                String random=IdUtil.simpleUUID();
                String extName= FileNameUtil.extName(file.getOriginalFilename()).toLowerCase();
                String fileName=random+"."+extName;
                //判断
                String[] allowUploadExt=new String[]{"jpg","png","gif"};
                if (Arrays.asList(allowUploadExt).contains(extName)) {
                    String savePath="/upload/";
                    try {
                        //真实地址
                        String  realPath= request.getSession().getServletContext().getRealPath("");
                        String  realOfUploadPath= realPath+savePath;
                        //创建文件夹
                        FileUtil.mkdir(realOfUploadPath);
                        file.transferTo(new File(realOfUploadPath + fileName));
                        //
                        Admin admin=(Admin)session.getAttribute("admin");
                        if (StringUtils.hasLength(admin.getPhoto())){
                            FileUtil.del(realPath+admin.getPhoto());
                        }
                        admin.setPhoto(savePath+fileName);
                        adminService.updateAdmin(admin);

                        responseData = ResponseData.success();
                    } catch (IOException e) {
                        responseData = ResponseData.error(e.getMessage());
                    }
                }else {
                    responseData=ResponseData.error("上传失败，格式不符合要求");
                }
            }else {
                responseData=ResponseData.error("上传失败，请选择上传文件");
            }
        }else {
            responseData=ResponseData.error("上传失败，请上传文件");
        }
        return responseData;
    }
}
